<?php

namespace app\modules\Product\models;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use app\modules\Product\models\Product;

/**
 * ProductSearch represents the model behind the search form about `app\modules\Product\models\Product`.
 */
class ProductSearch extends Product
{
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['begin_go_time','end_go_time','supplier_id'],'safe'],
            [['id', 'cruise_id', 'day', 'from_city', 'to_city', 'line', 'status', 'user_id', 'type'], 'integer'],
            [['code', 'go_time', 'return_time', 'name', 'cost', 'visa', 'created_at', 'updated_at'], 'safe'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        $query = Product::find();

        // add conditions that should always apply here

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
            //'sort' =>  ['defaultOrder'=> ['go_time'=> SORT_ASC]]
            'sort' =>  ['defaultOrder'=> ['go_time'=> SORT_DESC]]
        ]);

        if(empty($params) || !isset($params['ProductSearch'])){
            $params['ProductSearch']['status']  = 1;    
        }

        $this->load($params);

        if (!$this->validate()) {
            // uncomment the following line if you do not want to return any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }

        // grid filtering conditions
        $query->andFilterWhere([
            'id' => $this->id,
            'cruise_id' => $this->cruise_id,
            'go_time' => $this->go_time,
            'return_time' => $this->return_time,
            'day' => $this->day,
            'from_city' => $this->from_city,
            'to_city' => $this->to_city,
            'line' => $this->line,
            'type' => $this->type,
            'status' => $this->status,
            'user_id' => $this->user_id,
            'created_at' => $this->created_at,
            'updated_at' => $this->updated_at,

        ]);

        $query->andFilterWhere(['like', 'code', $this->code])
            ->andFilterWhere(['like', 'name', $this->name])
            ->andFilterWhere(['like', 'cost', $this->cost])
            ->andFilterWhere(['like', 'visa', $this->visa]);

        return $dataProvider;
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function line_search($params)
    {
        $query = Product::find();
         
        $query->joinWith('cruise'); 

        // add conditions that should always apply here

        $this->status = 1;

        $this->begin_go_time = date("Y-m-d",time());

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
            'pagination' => [
                'pageSize' => 999,
            ],
            'sort' =>  ['defaultOrder'=> ['go_time'=> SORT_ASC]]
        ]);
        $this->load($params);
        if (!$this->validate()) {
            // uncomment the following line if you do not want to return any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }

        // grid filtering conditions
        $query->andFilterWhere([
            'product.id' => $this->id,
            'product.cruise_id' => $this->cruise_id,
            //'go_time' => $this->go_time,
            'product.return_time' => $this->return_time,
            'product.day' => $this->day,
            'product.from_city' => $this->from_city,
            'product.to_city' => $this->to_city,
            'product.line' => $this->line,
            'product.type' => $this->type,
            'product.status' => $this->status,
            'product.user_id' => $this->user_id,
            'product.created_at' => $this->created_at,
            'product.updated_at' => $this->updated_at,
            'cruise.supplier_id'=> $this->supplier_id,
        ]);

        $query->andFilterWhere(['like', 'product.code', $this->code])
            ->andFilterWhere(['like', 'product.name', $this->name])
            ->andFilterWhere(['like', 'product.cost', $this->cost])
            ->andFilterWhere(['like', 'product.visa', $this->visa])
            ->andFilterWhere(['>=', 'product.go_time', $this->begin_go_time])
            ->andFilterWhere(['<=', 'product.go_time', $this->end_go_time]);
      

        return $dataProvider;
    }
}
